package com.example.botany_api.controller;

import com.alibaba.fastjson.JSON;
import com.example.botany_api.bean.Information;
import com.example.botany_api.service.InformationService;
import com.example.botany_api.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 漫画忧作
 * @Date 2022/11/5 1:40
 * @Version 1.0
 */
@RestController
public class InformationController {
    @Autowired
    private InformationService informationService;



    @Autowired
    OssUtil ossUtil;

    //存储图片地址
    @RequestMapping("/admin/setInformationImg")
    public String addImg(MultipartFile file){
        //调用上传阿里云oss
        String str=ossUtil.checkImage(file);
        System.out.println(str);
        return str;
    }
    //    管理员 获得植物养护
    @RequestMapping("/admin/informationList")
    public String informationList(@RequestBody HashMap req) {
        String name = req.get("name").toString();
        String type = req.get("type").toString();
        String summary = req.get("summary").toString();

        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", informationService.getInformation(name,summary,type,(current - 1) * size, size));
        res.put("total", informationService.getInformationTotal(name,summary,type));

        return JSON.toJSONString(res);
    }
    //    管理员 添加植物养护
    @RequestMapping("/admin/add_Information")
    public String add_Information(@RequestBody Information information) {
        int i = informationService.add_Information(information);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 修改
    @RequestMapping("/admin/up_Information")
    public String up_Information(@RequestBody Information information) {
        System.out.println(information);
        int i = informationService.up_Information(information);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 删除数据
    @RequestMapping("/admin/dl_Information")
    public String dl_Information(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = informationService.dl_Information(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
}
